##############################################################################################################################
# CREATE REGRESSION TABLES THAT REFLECT RESULTS OF COEFFICIENTS PLOTTED ON THE EXCLUSION RESTRICTION COVARIANCE CHECK FIGURE #
##############################################################################################################################
# Author: Kasia Nalewajko
# First created: 09 June 2024

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("estimatr")) install.packages("estimatr")
if (!require("modelsummary")) install.packages("modelsummary")

# LOAD DATA ---------------------------------------------------------------

load("./00 SUBMITTED/00 APSR final/04 replication_files/01 data/main.Rda")

# RUN MODELS --------------------------------------------------------------

# Generate vector of outcomes
outcomes <- c("log(turnout_19_pct+1)",
              "log(empty_ballots_19_pct+1)",
              "log(nuance_extremegauche_19_pct+1)",
              "log(nuance_gauche_19_pct+1)",
              "log(nuance_centregauche_19_pct+1)",
              "log(nuance_centredroit_19_pct+1)",
              "log(nuance_droite_19_pct+1)",
              "log(nuance_extremedroite_19_pct+1)",
              "log(nuance_divers_19_pct+1)",
              "log(turnout_24_pct+1)",
              "log(nuance_extremegauche_24_pct+1)",
              "log(nuance_gauche_24_pct+1)",
              "log(nuance_centregauche_24_pct+1)",
              "log(nuance_centredroit_24_pct+1)",
              "log(nuance_droite_24_pct+1)",
              "log(nuance_divers_24_pct+1)",
              "log(turnout_32_pct+1)",
              "log(empty_ballots_32_pct+1)",
              "log(nuance_extremegauche_32_pct+1)",
              "log(nuance_gauche_32_pct+1)",
              "log(nuance_centregauche_32_pct+1)",
              "log(nuance_centredroit_32_pct+1)",
              "log(nuance_droite_32_pct+1)",
              "log(nuance_divers_32_pct+1)",
              "log(turnout_36_1_pct+1)",
              "log(empty_ballots_36_1_pct+1)",
              "log(nuance_extremegauche_36_1_pct+1)",
              "log(nuance_gauche_36_1_pct+1)",
              "log(nuance_centregauche_36_1_pct+1)",
              "log(nuance_centredroit_36_1_pct+1)",
              "log(nuance_droite_36_1_pct+1)",
              "log(nuance_extremedroite_36_1_pct+1)",
              "log(nuance_divers_36_1_pct+1)",
              "log(collabos_antijew1000+1)"
)

models <- list()
covariates <- c(
  "log(mpf1000+1) + log(pop1911) + area_sqkm + longitude + longitudesq + latitude + latitudesq"
)

# Loop over outcomes
for (i in 1:length(outcomes)) {
  
  # Initialize a list to store models for the current outcome
  models[[i]] <- list()
  
  # Loop through each set of covariates
  for (j in 1:length(covariates)) {
    formula <- as.formula(paste(outcomes[i], "~", covariates[[j]]))
    models[[i]][[j]] <- lm_robust(formula, clusters = id_bureau, fixed_effects = as.factor(ar_name), data = main)
  }
  
  print(i)
}


# Export models with Election 1919 ----

modelsummary(list(
  "Turnout" = models[[1]][[1]], 
  "Empty ballots" = models[[2]][[1]], 
  "Extreme left" = models[[3]][[1]], 
  "Left" = models[[4]][[1]], 
  "Centre-left" = models[[5]][[1]],
  "Centre-right" = models[[6]][[1]], 
  "Right" = models[[7]][[1]], 
  "Extreme right" = models[[8]][[1]], 
  "Other votes" = models[[9]][[1]]
  ),
             stars = c('*' = .1, '**' = .05, '***' = .01),
             output = "latex",
             gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
             title = "Covariate balance check: exclusion restriction (1919 election)",
             coef_rename = c("log(pop1911)" = "1911 population",
                             "area_sqkm" = "Area size (km2)",
                             "longitude" = "Longitude",
                             "latitude" = "Latitude",
                             "longitudesq" = "Longitude (sq)",
                             "latitudesq" = "Latitude (sq)",
                             "log(mpf1000 + 1)" = "WWI military death rates")
             )

# Export models with Election 1924 ----

modelsummary(list(
  "Turnout" = models[[10]][[1]],
  "Empty left" = models[[11]][[1]],
  "Left" = models[[12]][[1]],
  "Centre-left" = models[[13]][[1]],
  "Centre-right" = models[[14]][[1]],
  "Right" = models[[15]][[1]], 
  "Other votes" = models[[16]][[1]]
  ),
                             stars = c('*' = .1, '**' = .05, '***' = .01),
                             output = "latex",
                             gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
             title = "Covariate balance check: exclusion restriction (1924 election)",
             coef_rename = c("log(pop1911)" = "1911 population",
                             "area_sqkm" = "Area size (km2)",
                             "longitude" = "Longitude",
                             "latitude" = "Latitude",
                             "longitudesq" = "Longitude (sq)",
                             "latitudesq" = "Latitude (sq)",
                             "log(mpf1000 + 1)" = "WWI military death rates"
             ))

# Export models with Election 1932 ----

modelsummary(list(
  "Turnout" = models[[17]][[1]], 
  "Empty ballots" = models[[18]][[1]], 
  "Extreme left" = models[[19]][[1]], 
  "Left" = models[[20]][[1]], 
  "Centre-left" = models[[21]][[1]],
  "Centre-right" = models[[22]][[1]],
  "Right" = models[[23]][[1]],
  "Other votes" = models[[24]][[1]]
  ),
             stars = c('*' = .1, '**' = .05, '***' = .01),
             output = "latex",
             gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
             title = "Covariate balance check: exclusion restriction (1932 election)",
             coef_rename = c("log(pop1911)" = "1911 population",
                             "area_sqkm" = "Area size (km2)",
                             "longitude" = "Longitude",
                             "latitude" = "Latitude",
                             "longitudesq" = "Longitude (sq)",
                             "latitudesq" = "Latitude (sq)",
                             "log(mpf1000 + 1)" = "WWI military death rates"
             ))

# Export models with Election 1936 ----

models[[34]][[1]][["outcome"]]

modelsummary(list(
  "Turnout" = models[[25]][[1]], 
  "Empty ballots" = models[[26]][[1]], 
  "Extreme left" = models[[27]][[1]], 
  "Left" = models[[28]][[1]], 
  "Centre-left" = models[[29]][[1]],
  "Centre-right" = models[[30]][[1]],
  "Right" = models[[31]][[1]],
  "Extreme right" = models[[32]][[1]],
  "Other votes" = models[[33]][[1]],
  "WW2 collaborators" = models[[34]][[1]]
  ),
             stars = c('*' = .1, '**' = .05, '***' = .01),
             output = "latex",
             gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
             title = "Covariate balance check: exclusion restriction (1936 election and collaboration)",
             coef_rename = c("log(pop1911)" = "1911 population",
                             "area_sqkm" = "Area size (km2)",
                             "longitude" = "Longitude",
                             "latitude" = "Latitude",
                             "longitudesq" = "Longitude (sq)",
                             "latitudesq" = "Latitude (sq)",
                             "log(mpf1000 + 1)" = "WWI military death rates"
             ))





